<?php

namespace app\api\controller;


class Article extends ApiBase {
    protected $beforeActionList = [
        'checkUserToken' => ['except' => 'index,read,update']
    ];

    public function index() {
        $query = input('get.');
        if ($query['type'] === 'banner') {
            $condition['is_banner'] = config('setting.is_banner');
        }
        if ($query['type'] === 'list') {
            $condition['is_banner'] = ['neq', config('setting.is_banner')];
        }
        $this->getPageAndSize($query);
        $result = model('Article')->getArticleList($condition, $this->from, $this->size);
        return show(0, '获取数据成功', $result);
    }

    public function read($id) {
        $result = model('Article')->getArticleDetail($id);
        model('Article')->get($id)->setInc('view_count');
        return show(0, '获取数据成功', $result);
    }

    public function update($id) {
        if (input('get.isadd/d') == 1) {
            model('Article')->updateCount($id, 'up_count');
        } else {
            model('Article')->updateCount($id, 'up_count', true);
        }
        return show(0, 'OK');
    }
}